/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.profile;

import org.openaecfm.model.profile.ReinforcementBarProperties;
import org.openaecfm.model.profile.ReinforcingBarRoleEnum;
import org.openaecfm.model.profile.SectionProperties;
import org.openaecfm.model.profile.SectionReinforcementProperties;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class SectionReinforcementPropertiesIO extends
		AbstractEntityInstanceInputImpl<SectionReinforcementProperties>
		implements IEntityInstanceOutput<SectionReinforcementProperties> {

	@Override
	public String exportSPF(SectionReinforcementProperties pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSECTIONREINFORCEMENTPROPERTIES",
				appDataManager.getName(pojo));

		// Set LongitudinalStartPosition
		ei.addParameter(new RealParameter(pojo.getLongitudinalStartPosition()));

		// Set LongitudinalEndPosition
		ei.addParameter(new RealParameter(pojo.getLongitudinalEndPosition()));

		// Set TransversePosition
		if (pojo.getTransversePosition() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTransversePosition()));
		}

		// Set ReinforcementRole
		ei.addParameter(new EnumerationParameter(pojo.getReinforcementRole()));

		// Set SectionDefinition
		ei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getSectionDefinition())));

		// Set CrossSectionReinforcementDefinitions
		ListParameter l = new ListParameter();
		for (ReinforcementBarProperties o : pojo
				.getCrossSectionReinforcementDefinitions()) {
			l.add(new EntityInstanceNameParameter(appDataManager
					.getName(o)));
		}
		ei.addParameter(l);

		return ei.toString();
	}

	@Override
	protected SectionReinforcementProperties doCreateObject() {
		return new SectionReinforcementProperties();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			SectionReinforcementProperties pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LongitudinalStartPosition
		RealParameter longitudinalStartPosition = (RealParameter) sei
				.getParameter(0);
		pojo.setLongitudinalStartPosition(longitudinalStartPosition.toDouble());

		// Set LongitudinalEndPosition
		RealParameter longitudinalEndPosition = (RealParameter) sei
				.getParameter(1);
		pojo.setLongitudinalEndPosition(longitudinalEndPosition.toDouble());

		// Set TransversePosition
		Parameter transversePosition = sei.getParameter(2);
		if (transversePosition.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) transversePosition;
			pojo.setTransversePosition(r.toDouble());
		}

		// Set ReinforcementRole
		EnumerationParameter reinforcementRole = (EnumerationParameter) sei
				.getParameter(3);
		pojo.setReinforcementRole(ReinforcingBarRoleEnum
				.valueOf(reinforcementRole.toEnumString()));

	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SectionReinforcementProperties pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SectionDefinition
		EntityInstanceNameParameter sectionDefinition = (EntityInstanceNameParameter) sei
				.getParameter(4);
		pojo.setSectionDefinition((SectionProperties) appDataManager
				.getObject(sectionDefinition.toInstanceName()));

		// Set CrossSectionReinforcementDefinitions
		ListParameter l = (ListParameter) sei.getParameter(5);
		for (Parameter p : l) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
			pojo.getCrossSectionReinforcementDefinitions().add(
					(ReinforcementBarProperties) appDataManager.getObject(e
							.toInstanceName()));
		}
	}
}
